package com.tensquare.friend.controller;
/*
 *ClassName:FriendController
 *Description:交友模块的控制器层
 *@author:可爱的大鸭子
 */

import com.tensquare.friend.pojo.Gath;
import com.tensquare.friend.pojo.User;
import com.tensquare.friend.service.FriendService;
import com.tensquare.friend.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 添加好友（单向添加）
     * @param friendid 对方用户ID
     * @param type 1：喜欢 0：不喜欢
     * @return
     */
    @RequestMapping(value="/like/{friendid}/{type}",method=RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid , @PathVariable String type){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null){
            return new Result(false, StatusCode.ACCESSERROR,"无权访问");
        }
        int flag=friendService.addFriend(claims.getId(),friendid,type);
        //如果是喜欢
        if(type.equals("1")||type.equals("2")){
            if(flag==0){
                return new Result(false, StatusCode.REPERROR,"已经取消喜欢该好友",flag);
            }
        }
        //不喜欢
        else if(type.equals("0")){
            friendService.addNoFriend(claims.getId(),friendid);//向不喜欢列表添加记录
        }
        //其他type
        else{
            return new Result(false, StatusCode.ERROR, "操作有误");
        }
        return new Result(true, StatusCode.OK, "操作成功",flag);
    }

    /**
     * 删除好友
     * @param friendid
     * @return
     */
    @RequestMapping(value="/{friendid}",method=RequestMethod.DELETE)
    public Result remove(@PathVariable String friendid){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null){
            return new Result(false, StatusCode.ACCESSERROR,"无权访问");
        }
        friendService.deleteFriend(claims.getId(), friendid);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //推荐好友列表，未登录的时候
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result listAll(){
        List<User> list=userService.findAll();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //查询我的喜欢人数和被喜欢的人数
    @RequestMapping(value = "/getLove",method = RequestMethod.GET)
    public Result getLove(){
        //获取当前登录的用户
        Claims user_claims=(Claims)request.getAttribute("user_claims");
        if(user_claims==null) return new Result(false,StatusCode.ERROR,"尚未登录无权访问");
        String id=user_claims.getId();
        Map<String,Integer> map =friendService.getMyLove(id);
        return new Result(true,StatusCode.OK,"查询成功",map);
    }

    //按照时间排序返回最新三个用户的活动
    @RequestMapping(value = "/findTop3",method = RequestMethod.GET)
    public Result findTop3(){
        List<User> list=userService.findTop3();
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //查询给我推荐的异性好友列表,已经是好友的不查询，根据匹配度划分，并分页
    @RequestMapping(value = "/commend/{page}/{size}",method = RequestMethod.GET)
    public Result getCommend(@PathVariable int page,@PathVariable int size){
        Claims claims=(Claims)request.getAttribute("user_claims");
        if(claims==null) return new Result(false,StatusCode.ERROR,"尚未登录");
        String id=claims.getId();
        //根据id查询出用户
        User user=userService.findById(id);
        int sex=user.getSex();
        List<User> list=userService.getCommend(id,sex,page,size);
        Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return (int)(o2.getSuit()-o1.getSuit());
            }
        });
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //查询我的好友列表，还要返回该好友对应未读的消息和数量
    @RequestMapping(value = "/findMyFriends",method = RequestMethod.GET)
    public Result findMyFriends(){
        Claims claims = (Claims)request.getAttribute("user_claims");
        if(claims==null) return new Result(false,StatusCode.ERROR,"无权访问");
        String userId=claims.getId();
        List<String> list=friendService.findMyFriends(userId);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
    //根据id查询好友列表
    @RequestMapping(value = "/findMyFriends/{userId}",method = RequestMethod.GET)
    public Result findMyFriends1(@PathVariable String userId){
        List<String> list=friendService.findMyFriends(userId);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    //查询活动排行榜
    @RequestMapping(value = "/hotGaths",method = RequestMethod.GET)
    public Result getHotGatherings(){
        List<Gath> list=userService.getHotGatherings();
        return new Result(true,StatusCode.OK,"查询成功",list.subList(0,4)); //左闭右开
    }



}
